package bb.lanxing.lib.devices.bryton.ncs.phone;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import bb.lanxing.lib.devices.bryton.ncs.ExtensionData;
import bb.lanxing.lib.devices.bryton.ncs.ExtensionDataAttributeValue;
import bb.lanxing.lib.devices.bryton.ncs.NotificationExtension;
import bb.lanxing.lib.devices.bryton.ncs.constant.CategoryID;
import bb.lanxing.lib.devices.bryton.ncs.constant.EventFlags;
import bb.lanxing.lib.devices.bryton.ncs.constant.EventID;
import java.text.SimpleDateFormat;
import java.util.Date;

public class IncomingCallExtension extends NotificationExtension {
    private static final String TAG = "IncomingCallExtension";
    private String mBody;

    @Override
    protected void onUpdateData(int i) {
    }

    @Override
    public void onInitialize(boolean z) {
        logi("onInitialize");
        super.onInitialize(z);
        if (!z) {
            logi("setExtension to MessageManager");
            MessageManager.getInstance().setExtension(this);
        }
    }

    public void addNotification(String str, boolean z) {
        ExtensionData extensionData = new ExtensionData();
        if (z) {
            String qureyContactName = qureyContactName(this, str);
            StringBuilder sb = new StringBuilder();
            sb.append(qureyContactName + " " + str);
            sb.append("\n");
            sb.append(SimpleDateFormat.getDateTimeInstance().format(new Date()));
            this.mBody = sb.toString();
            extensionData.setTitle("incoming call").setEventId(EventID.NOTIFICATION_ADDED).setMessage(this.mBody).setAttribute(ExtensionDataAttributeValue.ATTR_INCOMING_CALL_NAME, qureyContactName).setAttribute(ExtensionDataAttributeValue.ATTR_INCOMING_CALL_NUMBER, str).setCategoryID(CategoryID.INCOMING_CALL).setEventFlag(EventFlags.IMPORTANT);
        } else {
            extensionData.setTitle("incoming call").setEventId(EventID.NOTIFICATION_REMOVED).setMessage(this.mBody).setAttribute(ExtensionDataAttributeValue.ATTR_INCOMING_CALL_NONE, "").setCategoryID(CategoryID.INCOMING_CALL).setEventFlag(EventFlags.IMPORTANT);
        }
        publishUpdate(extensionData);
    }

    public String qureyContactName(Context context, String str) {
        Cursor query = context.getContentResolver().query(Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(str)), new String[]{"display_name"}, null, null, null);
        return query.moveToFirst() ? query.getString(0) : "";
    }

    private void loge(String str) {
        Log.e(TAG, str);
    }

    private void logi(String str) {
        Log.i(TAG, str);
    }
}
